package com.rdzn.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rdzn.mall.base.common.Result;
import com.rdzn.mall.center.entity.SmsCouponHistory;
import com.rdzn.mall.center.service.ISmsCouponHistoryService;
import com.rdzn.mall.portal.service.ICouponService;
import com.rdzn.mall.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
* @Description: sms_coupon_history
* @Author: jeecg-boot
* @Date:   2020-08-27
* @Version: V1.0
*/
@Api(tags="sms_coupon_history")
@RestController
@RequestMapping("/coupon")
@Slf4j
public class MSmsCouponHistoryController extends JeecgController<SmsCouponHistory, ISmsCouponHistoryService> {
   @Autowired
   private ISmsCouponHistoryService smsCouponHistoryService;
   @Autowired
   private IUserService userService;
   @Autowired
   private ICouponService couponService;

   /**
    * 分页列表查询
    *
    * @param smsCouponHistory
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "sms_coupon_history-分页列表查询")
   @ApiOperation(value="sms_coupon_history-分页列表查询", notes="sms_coupon_history-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(SmsCouponHistory smsCouponHistory,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       smsCouponHistory.setMemberId(userService.getCurrentMember().getId());
       QueryWrapper<SmsCouponHistory> queryWrapper = QueryGenerator.initQueryWrapper(smsCouponHistory, req.getParameterMap());
       Page<SmsCouponHistory> page = new Page<>(pageNo, pageSize);
       IPage<SmsCouponHistory> pageList = smsCouponHistoryService.page(page, queryWrapper);
       return Result.ok(couponService.page(pageList));
   }

   /**
    *   兑换
    *
    * @param smsCouponHistory
    * @return
    */
   @AutoLog(value = "sms_coupon_history-兑换")
   @ApiOperation(value="sms_coupon_history-兑换", notes="sms_coupon_history-兑换")
   @PostMapping(value = "/exchange")
   public Result<?> exchange(@RequestBody SmsCouponHistory smsCouponHistory) {
       return Result.ok(couponService.exchange(userService.getCurrentMember().getId(), smsCouponHistory.getCouponCode()));
   }

   /**
    *  编辑
    *
    * @param smsCouponHistory
    * @return
    */
   @AutoLog(value = "sms_coupon_history-编辑")
   @ApiOperation(value="sms_coupon_history-编辑", notes="sms_coupon_history-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody SmsCouponHistory smsCouponHistory) {
       smsCouponHistoryService.updateById(smsCouponHistory);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "sms_coupon_history-通过id删除")
   @ApiOperation(value="sms_coupon_history-通过id删除", notes="sms_coupon_history-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       smsCouponHistoryService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "sms_coupon_history-批量删除")
   @ApiOperation(value="sms_coupon_history-批量删除", notes="sms_coupon_history-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.smsCouponHistoryService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "sms_coupon_history-通过id查询")
   @ApiOperation(value="sms_coupon_history-通过id查询", notes="sms_coupon_history-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       SmsCouponHistory smsCouponHistory = smsCouponHistoryService.getById(id);
       if(smsCouponHistory==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(smsCouponHistory);
   }

}
